<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>校园公告列表</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
    <h1 class="mb-4">校园公告列表</h1>
    
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
        <div class="container-fluid">
            <a class="navbar-brand" href="/announcements">校园公告系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link active" href="/announcements">公告列表</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/announcements/add">新增公告</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/lb-test">负载均衡测试</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/feign-test">OpenFeign测试</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/circuit-test">熔断测试</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="http://127.0.0.1:9411/" target="_blank">链路跟踪</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    
    <!-- 搜索框 -->
    <div class="row mb-4">
        <div class="col-md-6">
            <form th:action="@{/announcements/search}" method="get" class="d-flex">
                <input type="text" name="keyword" th:value="${keyword}" class="form-control me-2" placeholder="搜索公告...">
                <button type="submit" class="btn btn-primary">搜索</button>
            </form>
        </div>
        <div class="col-md-6 text-end">
            <a href="/announcements/add" class="btn btn-primary">
                <i class="bi bi-plus-circle"></i> 新增公告
            </a>
        </div>
    </div>

    <!-- 错误消息显示 -->
    <div th:if="${param.error}" class="alert alert-danger alert-dismissible fade show" role="alert">
        <span th:text="${param.error}"></span>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>

    <!-- 公告列表 -->
    <div class="table-responsive">
        <form id="batchForm">
            <table class="table table-striped table-bordered">
                <thead class="table-dark">
                <tr>
                    <th>
                        <input type="checkbox" id="selectAll" class="form-check-input">
                    </th>
                    <th>ID</th>
                    <th>标题</th>
                    <th>作者</th>
                    <th>发布日期</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:if="${announcements.isEmpty()}">
                    <td colspan="7" class="text-center">暂无公告</td>
                </tr>
                <tr th:each="announcement : ${announcements}">
                    <td>
                        <input type="checkbox" name="ids" th:value="${announcement.id}" class="form-check-input">
                    </td>
                    <td th:text="${announcement.id}"></td>
                    <td th:text="${announcement.title}"></td>
                    <td th:text="${announcement.author}"></td>
                    <td th:text="${#dates.format(announcement.publishDate, 'yyyy-MM-dd HH:mm')}"></td>
                    <td>
                        <span th:class="${announcement.status == T(com.example.campusannouncement.entity.Announcement.Status).ACTIVE ? 'badge bg-success' : 'badge bg-secondary'}"
                              th:text="${announcement.status}"></span>
                    </td>
                    <td>
                        <a th:href="@{/announcements/edit/{id}(id=${announcement.id})}" class="btn btn-warning btn-sm">
                            <i class="bi bi-pencil"></i> 编辑
                        </a>
                        <button type="button" class="btn btn-danger btn-sm" 
                                th:onclick="'deleteAnnouncement(' + ${announcement.id} + ')'">
                            <i class="bi bi-trash"></i> 删除
                        </button>
                    </td>
                </tr>
                </tbody>
            </table>
        </form>
    </div>

    <!-- 批量操作按钮 -->
    <div class="mb-3">
        <button type="button" class="btn btn-danger" onclick="deleteSelected()">
            <i class="bi bi-trash"></i> 批量删除
        </button>
    </div>

    <!-- 分页 -->
    <nav th:if="${totalPages > 1}">
        <ul class="pagination justify-content-center">
            <li class="page-item" th:classappend="${currentPage == 0} ? 'disabled'">
                <a class="page-link" th:href="@{/announcements(page=${currentPage - 1}, size=10, sortBy=${param.sortBy}, direction=${param.direction})}">上一页</a>
            </li>
            <li class="page-item" th:each="i : ${#numbers.sequence(0, totalPages - 1)}"
                th:classappend="${currentPage == i} ? 'active'">
                <a class="page-link" th:href="@{/announcements(page=${i}, size=10, sortBy=${param.sortBy}, direction=${param.direction})}"
                   th:text="${i + 1}"></a>
            </li>
            <li class="page-item" th:classappend="${currentPage == totalPages - 1} ? 'disabled'">
                <a class="page-link" th:href="@{/announcements(page=${currentPage + 1}, size=10, sortBy=${param.sortBy}, direction=${param.direction})}">下一页</a>
            </li>
        </ul>
    </nav>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 全选/取消全选
    document.getElementById('selectAll').addEventListener('change', function() {
        document.querySelectorAll('input[name="ids"]').forEach(checkbox => {
            checkbox.checked = this.checked;
        });
    });

    // 删除单个公告
    function deleteAnnouncement(id) {
        if (confirm('确定要删除此公告吗？')) {
            window.location.href = '/announcements/delete/' + id;
        }
    }

    // 批量删除
    function deleteSelected() {
        const selectedIds = Array.from(document.querySelectorAll('input[name="ids"]:checked'))
            .map(checkbox => checkbox.value);
        
        if (selectedIds.length === 0) {
            alert('请选择要删除的公告');
            return;
        }

        if (confirm('确定要删除选中的公告吗？')) {
            fetch('/announcements/delete/batch', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(selectedIds)
            })
            .then(response => response.json())
            .then(data => {
                alert(data);
                window.location.reload();
            })
            .catch(error => {
                console.error('Error:', error);
                alert('删除失败，请重试');
            });
        }
    }
</script>
</body>
</html>